package com.bw.module_store.viewmodel

import androidx.lifecycle.MutableLiveData
import com.bw.lib_common.coroutine.CoroutineHelper
import com.bw.lib_common.vm.BaseViewModel2
import com.bw.module_store.entity.AddStoreParam
import com.bw.module_store.entity.AddStoreResult
import com.bw.module_store.entity.StoreDetailResult
import com.bw.module_store.entity.StoreResult
import com.bw.module_store.repo.StoreRepo
import kotlinx.coroutines.launch
/**
 * 智慧园区-01-商家推荐-v1.1-吴熙-20250514
 */
class StoreViewModel:BaseViewModel2<StoreRepo>() {

    val getStoreListSuccess by lazy { MutableLiveData<List<StoreResult>>() }
    val getStoreDetailSuccess by lazy { MutableLiveData<StoreDetailResult>() }
    val addStoreInSuccess by lazy { MutableLiveData<AddStoreResult>() }
    val getMerchantListSuccess by lazy { MutableLiveData<List<AddStoreResult>>() }
    val getMerchantDetailSuccess by lazy { MutableLiveData<AddStoreResult>() }

    fun getStoreList(){
        CoroutineHelper.mViewModelScope.launch(CoroutineHelper.mDispatcher){
            val result = mRepository.getStoreList()
            if (result.code == 200){
                getStoreListSuccess.postValue(result.data)
            } else {
                mErrorLiveData.postValue(result.message)
            }
        }
    }

    fun getStoreDetail(storeId:Int){
        CoroutineHelper.mViewModelScope.launch(CoroutineHelper.mDispatcher){
            val result = mRepository.getStoreDetail(storeId)
            if (result.code == 200){
                getStoreDetailSuccess.postValue(result.data)
            } else {
                mErrorLiveData.postValue(result.message)
            }
        }
    }

    fun addStoreIn(storeParam: AddStoreParam){
        CoroutineHelper.mViewModelScope.launch(CoroutineHelper.mDispatcher){
            val result = mRepository.addStoreIn(storeParam)
            if (result.code == 200){
                addStoreInSuccess.postValue(result.data)
            } else {
                mErrorLiveData.postValue(result.message)
            }
        }
    }


    fun getMerchantList(){
        CoroutineHelper.mViewModelScope.launch(CoroutineHelper.mDispatcher){
            val result = mRepository.getMerchantList()
            if (result.code == 200){
                getMerchantListSuccess.postValue(result.data)
            } else {
                mErrorLiveData.postValue(result.message)
            }
        }
    }


    fun getMerchantDetail(merchantId:Int){
        CoroutineHelper.mViewModelScope.launch(CoroutineHelper.mDispatcher){
            val result = mRepository.getMerchantDetail(merchantId)
            if (result.code == 200){
                getMerchantDetailSuccess.postValue(result.data)
            } else {
                mErrorLiveData.postValue(result.message)
            }
        }
    }


    override fun createRepository(): StoreRepo = StoreRepo()

}